home *** CD-ROM | disk | FTP | other *** search
- // MainWindow.cpp: Implementierungsdatei
- //
- // Copyright by AndrΘ Stein
- // E-Mail: stonemaster@steinsoft.net, andre_stein@web.de
- // http://www.steinsoft.net
- //////////////////////////////////////////////////////////////////////
-
- #include "stdafx.h"
- #include "OpenGLMfc.h"
- #include "MainWindow.h"
-
- #include <stdlib.h>
- #include <time.h>
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
-
- #define ROT_SPEED 0.03f
- #define ZOOM_SPEED 0.001f
-
- //** LICHT **
- const float WHOLE_SCENE_LIGHT[] = { 0.25f, 0.25f, 0.25f, 1.0f};
- const float DIFFUSE_LIGHT[] = { 0.6f, 0.6f, 0.6f, 1.0f};
- const float SPECULAR_LIGHT[] = {-.1f, -0.1f, -0.1f, 1.0f};
- const float LIGHT_POSITION1[] = { 0.0f, -5.0f,0.0f, 1.0f};
- const float LIGHT_POSITION2[] = { 0.0f, 5.0f,0.0f, 1.0f};
-
- const float SPOT_DIRECTION[] = {0.0f,0.0f,-1.0f};
- const float MATERIAL[] = {1.0f,1.0f,1.0f,1.0f};
-
- /////////////////////////////////////////////////////////////////////////////
- // CMainWindow
-
- CMainWindow::CMainWindow()
- {
- clientDC = NULL;
- fullscreen = true;
- mousePosition[0] = 0.0f;
- mousePosition[1] = 0.0f;
- mouseDown = false;
- movement[0] = -8.0f;
- movement[1] = 0.0f;
-
- demoState = STATE_INTRO;
-
- //rand() initialisieren
- srand(time(NULL));
- }
-
- CMainWindow::~CMainWindow()
- {
- if (clientDC)
- {
- delete clientDC;
- }
- }
-
-
- BEGIN_MESSAGE_MAP(CMainWindow, CWnd)
- //{{AFX_MSG_MAP(CMainWindow)
- ON_WM_SIZE()
- ON_WM_CLOSE()
- ON_WM_SYSCOMMAND()
- ON_WM_CREATE()
- ON_WM_KEYDOWN()
- ON_WM_SETFOCUS()
- ON_WM_KILLFOCUS()
- ON_WM_LBUTTONDOWN()
- ON_WM_LBUTTONUP()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
-
- /////////////////////////////////////////////////////////////////////////////
- // Behandlungsroutinen fⁿr Nachrichten CMainWindow
-
- void CMainWindow::OnSize(UINT nType, int cx, int cy)
- {
- CWnd::OnSize(nType, cx, cy);
-
- if (cy == 0)
- {
- cy = 1;
- }
-
- glViewport(0, 0, cx, cy);
-
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
-
-
- gluPerspective(45.0f,(GLfloat)cx/(GLfloat)cy,0.1f,100.0f);
-
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- }
-
- void CMainWindow::Update()
- {
- //***MOUSE***
-
- static CPoint lastMouse;
-
- GetCursorPos(&lastMouse);
- SetCursorPos(320,240);
-
- float moveX = float((320 - lastMouse.x))/100.0f;
- float moveY = float((240 - lastMouse.y))/100.0f;
-
- if (!(mousePosition[0] >= 5.0f || mousePosition[0] <= -5.0f ||
- mousePosition[1] >= 4.0f || mousePosition[1] <= -4.0f))
- {
- //Die Mausbewegung aktualisieren
- mousePosition[0] -= moveX;
- mousePosition[1] += moveY;
- }
- else
- {
- mousePosition[0] = -mousePosition[0] -
- (mousePosition[0] < 0.0f ? 0.05f : -0.05f);
- mousePosition[1] = -mousePosition[1] -
- (mousePosition[1] < 0 ? 0.05f:-0.05f);
- }
-
-
-
- //Frametime ausrechnen
- static float framestart = timer.getTime();
- static float frameend = 0.0f;
-
- do
- {
- frameend = timer.getTime();
- }
- while (frameend == framestart);
-
- frametime = frameend - framestart;
- framestart = frameend;
-
- if (GetAsyncKeyState(VK_ESCAPE))
- {
- if (demoState == STATE_GAME || demoState == STATE_END ||
- demoState == STATE_MOVING)
- {
- PostMessage(WM_CLOSE);
- }
- else if (demoState == STATE_INTRO)
- {
- demoState = STATE_GAME;
- /**/
- Sleep(500);
- }
-
- }
-
- //***************
- // Treefield updaten
- if (demoState == STATE_GAME)
- {
- if (mouseDown)
- {
- treefield.move(mousePosition[0],mousePosition[1]);
- }
-
- if (treefield.isDone())
- {
- demoState = STATE_MOVING;
- }
- }
- else if (demoState == STATE_INTRO)
- {
- if (intro.isDone())
- {
- //FadeOut(750);
- demoState = STATE_GAME;
- }
- }
- else if (demoState == STATE_MOVING)
- {
- movement[0] += ZOOM_SPEED*frametime;
- movement[1] += ROT_SPEED*frametime;
-
- if (movement[0] >= 2.0f)
- {
- demoState = STATE_END;
- endScene.create(clientDC->m_hDC,treefield.treesLeft());
- soundSystem.stopMusic(0);
- soundSystem.playMusic(STREAM_BELLS);
- }
- }
- else
- {
- if (endScene.isDone())
- {
- PostQuitMessage(0);
- }
- }
-
-
- DrawGLScene();
- }
-
- void CMainWindow::DrawGLScene()
- {
- glClear(GL_COLOR_BUFFER_BIT |
- GL_DEPTH_BUFFER_BIT);
-
- glLoadIdentity();
-
- if (demoState == STATE_GAME)
- {
- ////////////////////
- // Drawing code here
- ////////////////////
-
- glTranslatef(0.0f,0.0f,-8.0f);
-
- treefield.update(frametime);
- snowflakes.update(frametime);
-
- DrawMouseCursor();
-
- }
- else if (demoState == STATE_INTRO)
- {
- //TRACE0("ZEICHE INTRO...\n");
- intro.update(frametime);
- }
- else if (demoState == STATE_MOVING)
- {
- snowflakes.update(frametime);
-
- glTranslatef(0.0f,0.0f,movement[0]);
- glRotatef(movement[1],0.0f,0.0f,1.0f);
-
- treefield.update(frametime);
-
- }
- else
- {
- endScene.update(frametime);
- }
-
- SwapBuffers(clientDC->m_hDC);
- }
-
- void CMainWindow::OnClose()
- {
- screenMode.reset();
-
- PostQuitMessage(0);
-
- ShowCursor(true);
-
- CWnd::OnClose();
- }
-
- void CMainWindow::InitGL()
- {
- glShadeModel(GL_SMOOTH);
- glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
- glClearDepth(1.0f);
- glEnable(GL_DEPTH_TEST);
- glDepthFunc(GL_LEQUAL);
- glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
-
- glBlendFunc(GL_ONE,GL_ONE);
-
- glEnable(GL_TEXTURE_2D);
-
- /************************
- LIGHTING
- *************************/
-
- glLightModelfv(GL_LIGHT_MODEL_AMBIENT,WHOLE_SCENE_LIGHT);
-
- //Main
- glLightfv(GL_LIGHT0,GL_AMBIENT,SPECULAR_LIGHT);
- glLightfv(GL_LIGHT0,GL_SPECULAR,SPECULAR_LIGHT);
- glLightfv(GL_LIGHT0,GL_DIFFUSE,DIFFUSE_LIGHT);
- glLightfv(GL_LIGHT0,GL_POSITION,LIGHT_POSITION1);
-
- glLightfv(GL_LIGHT1,GL_AMBIENT,SPECULAR_LIGHT);
- glLightfv(GL_LIGHT1,GL_SPECULAR,SPECULAR_LIGHT);
- glLightfv(GL_LIGHT1,GL_DIFFUSE,DIFFUSE_LIGHT);
- glLightfv(GL_LIGHT1,GL_POSITION,LIGHT_POSITION2);
-
- //Spotlight
- glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 10.0f); //60░ Spotlight
- glLightf(GL_LIGHT0,GL_SPOT_EXPONENT, 60.0f);
- glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,SPOT_DIRECTION);
-
-
- glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
-
- //Materials
- glMaterialfv(GL_FRONT,GL_SPECULAR,MATERIAL);
- glMaterialf(GL_FRONT,GL_SHININESS,32.0f);
-
- glEnable(GL_COLOR_MATERIAL);
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- glEnable(GL_LIGHT1);
-
-
- ///////////////////////////
- // Textures
- ///////////////////////////
-
- textures[TEX_BAUM].createMipmap("Data/BaumOben.bmp");
- textures[TEX_BAUM_STAMM].createMipmap("Data/BaumStamm.bmp");
- textures[TEX_SNOWFLAKE].createMipmap("Data/Snowflake.bmp");
- textures[TEX_FLASCHE].createMipmap("Data/Flasche.bmp");
-
- treefield.create(&textures[TEX_BAUM],&textures[TEX_BAUM_STAMM],&soundSystem);
- snowflakes.create(&textures[TEX_SNOWFLAKE]);
- intro.create(clientDC->GetSafeHdc());
- soundSystem.init(m_hWnd);
-
- soundSystem.playMusic(STREAM_RUDI);
-
- /////////////////////////////////////////////////////////////////
-
- timer.create();
- }
-
- void CMainWindow::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if (nID == SC_SCREENSAVE ||
- nID == SC_MONITORPOWER)
- {
- return;
- }
-
- CWnd::OnSysCommand(nID, lParam);
- }
-
- void CMainWindow::CreateGLWindow()
- {
- if (fullscreen)
- {
- screenMode.setDisplayMode(640,480,16);
- }
-
-
- //Windowcreation
- CString className = AfxRegisterWndClass(
- CS_HREDRAW | CS_VREDRAW | CS_OWNDC,
- NULL,
- (HBRUSH)GetStockObject(BLACK_BRUSH),
- AfxGetApp()->LoadIcon(IDR_MAINFRAME));
-
- CreateEx(
- 0,
- className,
- "OpenGL",
- (fullscreen ? WS_POPUP : WS_POPUP|WS_CAPTION),
- (fullscreen ? CRect(0,0,640,480) : CRect(50,50,640,480)),
- NULL,
- 0);
-
- //Show Window and make active
- ShowWindow(SW_SHOW);
- SetForegroundWindow();
- SetFocus();
-
-
- //No cursor visible
- ShowCursor(false);
- }
-
- int CMainWindow::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CWnd::OnCreate(lpCreateStruct) == -1)
- return -1;
-
- //OpenGL init
-
- clientDC = new CClientDC(this);
- openGLDevice.create(clientDC->m_hDC);
- InitGL();
-
- return 0;
- }
-
- void CMainWindow::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- switch (nChar)
- {
- case VK_F9:
- fullscreen = !fullscreen;
- UpdateView();
- break;
- case VK_F7:
- Minimize();
- break;
- }
-
- CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
- }
-
- void CMainWindow::UpdateView()
- {
- if (fullscreen)
- {
- screenMode.setDisplayMode(640,480,16);
-
- ModifyStyle(WS_CAPTION,0);
- MoveWindow(CRect(0,0,640,480));
- }
- else
- {
- screenMode.reset();
-
- ModifyStyle(0,WS_CAPTION);
- MoveWindow(CRect(50,50,640,480));
- }
-
-
- }
-
- void CMainWindow::OnSetFocus(CWnd* pOldWnd)
- {
- CWnd::OnSetFocus(pOldWnd);
-
- UpdateView();
- }
-
- void CMainWindow::OnKillFocus(CWnd* pNewWnd)
- {
- CWnd::OnKillFocus(pNewWnd);
-
- Minimize();
-
- }
-
- void CMainWindow::Minimize()
- {
- if (IsIconic())
- {
- return;
- }
-
- WINDOWPLACEMENT placement;
- GetWindowPlacement(&placement);
-
- if (fullscreen)
- {
- screenMode.reset();
- }
-
- placement.showCmd = SW_SHOWMINIMIZED;
-
- SetWindowPlacement(&placement);
- }
-
-
- void CMainWindow::DrawMouseCursor()
- {
- glPushMatrix();
-
- glEnable(GL_BLEND);
- glDisable(GL_DEPTH_TEST);
-
- textures[TEX_FLASCHE].select();
-
- glBegin(GL_QUADS);
-
- glTexCoord2f(0.0f,1.0f); glVertex3f(mousePosition[0],mousePosition[1],
- (mouseDown ? 0.75f: 0.0f));
- glTexCoord2f(0.0f,0.0f); glVertex3f(mousePosition[0],mousePosition[1]-1.0f,
- (mouseDown ? 0.75f: 0.0f));
- glTexCoord2f(1.0f,0.0f); glVertex3f(mousePosition[0]+0.75f,mousePosition[1]-1.0f,
- (mouseDown ? 0.75f: 0.0f));
- glTexCoord2f(1.0f,1.0f); glVertex3f(mousePosition[0]+0.75f,mousePosition[1],
- (mouseDown ? 0.75f: 0.0f));
- glEnd();
-
- glDisable(GL_BLEND);
- glEnable(GL_DEPTH_TEST);
-
- glPopMatrix();
- }
-
- void CMainWindow::OnLButtonDown(UINT nFlags, CPoint point)
- {
- if (demoState == STATE_GAME)
- {
- if (treefield.click(mousePosition[0],mousePosition[1]))
- {
- mouseDown = true;
- }
- }
-
- CWnd::OnLButtonDown(nFlags, point);
- }
-
- void CMainWindow::OnLButtonUp(UINT nFlags, CPoint point)
- {
- if (demoState == STATE_GAME)
- {
- mouseDown = false;
- treefield.release(mousePosition[0],mousePosition[1]);
- }
-
- CWnd::OnLButtonUp(nFlags, point);
- }
-
- void CMainWindow::FadeOut(int delay)
- {
- STimer timer;
- timer.create();
-
- float time = 0.0f;
- float alpha = 0.0f;
-
-
- glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
- glEnable(GL_BLEND);
- glDisable(GL_DEPTH_TEST);
-
- for (;;)
- {
- //Zeit festhalten und ms seit Anfang dazurechnen
- time += timer.getTime();
-
- //Normales Qudrad zeichnen dass
- //je nach Fadelevel durchsichtig ist
- //(Position = try-and-error ;))
- glBegin(GL_QUADS);
- glColor4f(0.0f,0.0f,0.0f,alpha);
- glVertex3f(1.15f,1.15f,-2.0f);
- glVertex3f(-1.15f,1.15f,-2.0f);
- glVertex3f(-1.15f,-1.15f,-2.0f);
- glVertex3f(1.15f,-1.15f,-2.0f);
- glEnd();
-
- glFlush();
- SwapBuffers(clientDC->m_hDC);
-
- //Alpha wird berrechnet nach Relation zwischen
- //abgelaufener Zeit(time) und dem Limit(delay)
- alpha = (time/delay)/1000;
-
- if (alpha >= 1.0f)
- {
- break;
- }
- }
-
- glDisable(GL_BLEND);
- glEnable(GL_DEPTH_TEST);
-
- glBlendFunc(GL_ONE,GL_ONE);
- }